package com.china08.yxyapi.api.manage.tec.office.stuattendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.attendance.YxyStuAttendance;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.attendance.YxyStuAttendanceRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 晨检考勤查询
 * 
 */
@CrossOrigin
@RestController
public class YxyStuAttendanceSchController extends YxyApiBaseController {

    @Autowired
    private YxyStuAttendanceRepository yxyStuAttendanceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/stuattendance/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) {
        List<YxyStuAttendance> list = yxyStuAttendanceRepository.findByClassIdAndSchoolIdAndDayDate(model.classId,
                getProfile(request, response).getSchoolId(), DateUtils.formatDate4(new Date()));

        List<RespModel> rerutnList = new ArrayList<RespModel>();
        for (YxyStuAttendance ysa : list) {
            RespModel respModel = new RespModel();
            respModel.id = ysa.getId();
            respModel.studentId = ysa.getStudentId();
            respModel.stuName = ysa.getStudentName();
            String faceImgString = ysa.getAvatar();
            faceImgString = StringUtils.isBlank(faceImgString)
                    ? "https://yun.91yunxiao.com/static/img/default-avatar.png" : faceImgString;
            faceImgString = faceImgString.startsWith("http") || faceImgString.startsWith("https") ? faceImgString
                    : "https://yxcsp.91yunxiao.com/yxcsp/pull/" + faceImgString;
            respModel.avatar = faceImgString;
            respModel.stuNo = ysa.getStudentNo() == null ? null : ysa.getStudentNo().toString();
            respModel.signInStatus = ysa.getSignOutStatus() != 1 ? ysa.getSignInStatus() : -1;
            respModel.inspection = ysa.getInspection();
            respModel.signClass = ysa.getSignClass();
            rerutnList.add(respModel);
        }
        return rerutnList;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 班级id
        public String classId;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 学生id
        public String studentId;
        // 学生姓名
        public String stuName;
        // 头像
        public String avatar;
        // 学号
        public String stuNo;
        // 签到状态
        public Integer signInStatus;
        // 晨检状态
        public String inspection;
        // 到班状态
        public Integer signClass;
    }
}
